package cn.smvc.wechat.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cn.smvc.common.utils.DateUtil;
import cn.smvc.common.web.BaseServlet;
import cn.smvc.wechat.pojo.Token;
import cn.smvc.wechat.test.entity.WeChatAccount;
import cn.smvc.wechat.test.service.WeChatAccountService;
import cn.smvc.wechat.util.CommonUtil;


@WebServlet("/send/msg")
public class SendMessageServlet extends BaseServlet{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -1123111769158574898L;
	
	@Autowired
	private WeChatAccountService weChatAccountService;

	public void sentTask(HttpServletRequest request, HttpServletResponse response) {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json; charset=utf-8");
		
		PrintWriter out;
		try {
			Map<String, String[]> requestMap =  request.getParameterMap();
			String accountId = requestMap.get("accountId")[0];
			String openId = requestMap.get("openId")[0];
			String phone = requestMap.get("phone")[0];
			if(StringUtils.isNotEmpty(phone)) {
				
			}
			String taskId = requestMap.get("taskId")[0];
			String taskGroup = requestMap.get("taskGroup")[0];
			String createTime = requestMap.get("createTime")[0];
			String type = requestMap.get("type")[0];
			createTime = DateUtil.formatDateTime(new Date(new Long(createTime)));
			WeChatAccount account = weChatAccountService.get(Integer.parseInt(accountId));
			Token token = CommonUtil.getToken(accountId, account.getAppId(), account.getAppsecret());
			CommonUtil.sendTaskTempMsg(openId, taskId, taskGroup, createTime, type, token.getAccessToken());
			
			out = response.getWriter();
			out.println(JSON.toJSONString(""));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	public void sent(HttpServletRequest request, HttpServletResponse response) {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json; charset=utf-8");
		
		PrintWriter out;
		try {
			Map<String, String[]> requestMap =  request.getParameterMap();
			String accountId = requestMap.get("accountId")[0];
			String templateId = requestMap.get("templateId")[0];
			String openId = requestMap.get("openId")[0];
			String data = requestMap.get("data")[0];
			
			JSONObject json = JSON.parseObject(data);
			
			WeChatAccount account = weChatAccountService.get(Integer.parseInt(accountId));
			Token token = CommonUtil.getToken(accountId, account.getAppId(), account.getAppsecret());
			CommonUtil.sendTempMsg(templateId, openId, json, token.getAccessToken());
			
			out = response.getWriter();
			out.println(JSON.toJSONString(""));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
